//
//  LSSettingsViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/19.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSSettingsViewController.h"
#import "LSLoginViewController.h"
#import "LSMineTableViewCell.h"
#import "LSMineCenterLabelCell.h"
#import "LSForgetPasswordViewController.h"

@interface LSSettingsViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *settingsTableView;
@property (nonatomic, strong) NSString *currentVolum;
@end

static NSString *LSSettingsViewControllerCellID = @"LSSettingsViewControllerCellID";
static NSString *LSMineCenterLabelCellID = @"LSMineCenterLabelCellID";

@implementation LSSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    [self.view addSubview:self.settingsTableView];

    // 获取缓存大小
    NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
    self.currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
}

//计算出大小
- (NSString *)fileSizeWithInterge:(NSInteger)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}

- (void)goLoginButtonClick {
    WS
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"确定退出？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf goLogin];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:actionCancel];
    [alertC addAction:confirmAction];
    [self presentViewController:alertC animated:true completion:nil];
}

- (void)goLogin {
    [[UserInfoModel sharedInstance] logOut];
    [[NSNotificationCenter defaultCenter] postNotificationName:UINotificationWillGoLoginMessageName object:nil];
    [self.navigationController popToRootViewControllerAnimated:false];
}

- (void)shiftAccountCellClick {
    WS
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"确定切换账号？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:NO forKey:@"doRememberMe"];
        [defaults removeObjectForKey:UserLoginNameKey];
        [defaults removeObjectForKey:UserLoginPasswordKey];
        [defaults synchronize];
        [weakSelf goLogin];
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:actionCancel];
    [alertC addAction:confirmAction];
    [self presentViewController:alertC animated:true completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 2) {
        LSMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LSSettingsViewControllerCellID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.titleLabelName = @"修改密码";
        } else {
            cell.titleLabelName = @"清除缓存";
            cell.subTitleLabelName = self.currentVolum;
        }
        return cell;
    } else {
        LSMineCenterLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:LSMineCenterLabelCellID forIndexPath:indexPath];
        if (indexPath.row == 2) {
            cell.centerLabelText = @"切换账号";
        } else {
            cell.centerLabelText = @"退出登录";
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if (indexPath.row < 2) {
        if (indexPath.row == 0) {
            LSForgetPasswordViewController *forgetPasswordVC = [[LSForgetPasswordViewController alloc] init];
            [self.navigationController pushViewController:forgetPasswordVC animated:true];
        } else {
            [self deleteCellItemClick];
        }
    } else {
        if (indexPath.row == 2) {
            [self shiftAccountCellClick];
        } else {
            [self goLoginButtonClick];
        }
    }
}

- (void)deleteCellItemClick {
    WS
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定要清除缓存吗？" preferredStyle:UIAlertControllerStyleAlert];
    [self addActionTarget:alert title:@"取消" color:hexColor(333333) action:nil];
    [self addActionTarget:alert title:@"删除" color:NAV_COLOR action:^(UIAlertAction *action) {
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [MBProgressHUD showTipMessageInWindow:@"清除完毕"];
            NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
            weakSelf.currentVolum = [NSString stringWithFormat:@"%@",[weakSelf fileSizeWithInterge:intg]];
            [weakSelf.settingsTableView reloadData];
        }];
    }];
    [self presentViewController:alert animated:YES completion:nil];
}

// 取消按钮
- (void)addCancelActionTarget:(UIAlertController*)alertController color:(UIColor *)color title:(NSString *)title
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:nil];
    [action setValue:color forKey:@"_titleTextColor"];
    [alertController addAction:action];
}

// 选择按钮
- (void)addActionTarget:(UIAlertController *)alertController title:(NSString *)title color:(UIColor *)color action:(void(^)(UIAlertAction *action))actionTarget
{
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if (actionTarget) {
            actionTarget(action);
        }
    }];
    [action setValue:color forKey:@"_titleTextColor"];
    [alertController addAction:action];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 2) {
        return LSMineTableViewCellHeight;
    } else {
        return LSMineCenterLabelCellHeight;
    }
}

- (UITableView *)settingsTableView {
    if (!_settingsTableView) {
        _settingsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight-49)];
        UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
        tableViewHeaderView.backgroundColor = hexColor(EEEEEE);
        _settingsTableView.tableHeaderView = tableViewHeaderView;
        _settingsTableView.separatorInset = UIEdgeInsetsZero;
        _settingsTableView.tableFooterView = [UIView new];
        _settingsTableView.backgroundColor = hexColor(EEEEEE);
        _settingsTableView.delegate = self;
        _settingsTableView.dataSource = self;
        [_settingsTableView registerClass:LSMineTableViewCell.class forCellReuseIdentifier:LSSettingsViewControllerCellID];
        [_settingsTableView registerClass:LSMineCenterLabelCell.class forCellReuseIdentifier:LSMineCenterLabelCellID];
    }
    return _settingsTableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
